package com.tianlaiedu.controller;

import com.alibaba.fastjson.JSON;
import com.tianlaiedu.bean.Labell;
import com.tianlaiedu.bean.Relation;
import com.tianlaiedu.bean.ResultVO;
import com.tianlaiedu.bean.User;
import com.tianlaiedu.service.impl.LabellServiceImpl;
import com.tianlaiedu.service.impl.PostServiceImpl;
import com.tianlaiedu.service.impl.RelationImpl;
import com.tianlaiedu.service.impl.UserServiceImpl;
import com.tilaiedu.framework.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:hejing
 * @date:2021/12/1
 */
public class Post {
    private static final Logger LOGGER = LoggerFactory.getLogger(Login.class);
    @RequestMapping("/post/create")
    public void createPostInfo(HttpServletRequest request, HttpServletResponse response) throws IOException, ServerException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        // 通过json功能类来获取json数据
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);
        if (res==null){
            response.getWriter().write(JSON.toJSONString(resultVO));
            return;
        }

        //添加标签
        Labell labell = new Labell();
        labell.setComment((String) res.get("tabel"));
        LabellServiceImpl labellService = new LabellServiceImpl();
           //查看标签是否在数据库中存在
        Labell tabelShow = labellService.findByname("tabel");
        if (tabelShow==null){
            int i = labellService.addLabeel(labell);
            if(i<=0){
                response.getWriter().write(JSON.toJSONString(resultVO));
                return;
            }
            //增加帖子
            com.tianlaiedu.bean.Post post = new com.tianlaiedu.bean.Post();
            post.setUser_id((Integer) res.get("userid"));
         //   post.setUser_id(Integer.parseInt((String) res.get("id")));
            post.setTitle((String) res.get("postTitle"));
            post.setContent((String) res.get("content"));
            PostServiceImpl postService=new PostServiceImpl();
            int addPost = postService.addPost(post);
            if (addPost<=0){
                response.getWriter().write(JSON.toJSONString(resultVO));
                return;
            }

            //绑定标签和帖子id
            Relation relation = new Relation();
//        postid
            post=postService.finByname((String)res.get("postTitle"));
            relation.setPost_id(post.getId());
//        labellid
            labell=labellService.findByname((String) res.get("tabel"));
            relation.setLabel_id(labell.getId());
            RelationImpl relation1=new RelationImpl();
            int re = relation1.addRelatin(relation);
            if (re<=0){
                response.getWriter().write(JSON.toJSONString(resultVO));
                return;
            }
//        返回信息

            resultVO.setType(0);
            response.getWriter().write(JSON.toJSONString(resultVO));
        }else {
            //增加帖子
            com.tianlaiedu.bean.Post post2 = new com.tianlaiedu.bean.Post();
            post2.setUser_id((Integer) res.get("userid"));
            post2.setTitle((String) res.get("postTitle"));
            post2.setContent((String) res.get("content"));
            PostServiceImpl postService=new PostServiceImpl();
            int addPost = postService.addPost(post2);
            if (addPost<=0){
                response.getWriter().write(JSON.toJSONString(resultVO));
                return;
            }
            //绑定标签和帖子id
            Relation relation = new Relation();
//        postid赋值
            post2=postService.finByname((String)res.get("postTitle"));
            relation.setPost_id(post2.getId());
//        labellid赋值
            tabelShow=labellService.findByname((String) res.get("tabel"));
            relation.setLabel_id(tabelShow.getId());
            RelationImpl relation1=new RelationImpl();
            int re = relation1.addRelatin(relation);
            if (re<=0){
                response.getWriter().write(JSON.toJSONString(resultVO));
                return;
            }
            //        返回信息

            resultVO.setType(0);
            response.getWriter().write(JSON.toJSONString(resultVO));
        }

    }
    @RequestMapping("/post/look")
    public void getPostInfo(HttpServletRequest request,HttpServletResponse response)throws IOException,ServerException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        // 通过json功能类来获取json数据
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);

            //查看主页
            // 创建一个map，用来保存参数
            HashMap<String, Object> param = new HashMap<>();

            // 获取当前的显示的页数
            String page = (String) res.get("page");
            if (page == null || page.isEmpty()){
                // 设置默认的页数
                page = "1";
            }
            param.put("page",Integer.parseInt(page));
            // 获取一个当前一页要显示的数据条数
            String limit = request.getParameter("limit");
            if (limit == null || limit.isEmpty()){
                // 设置默认的没有显示条数
                limit = "10";
            }
            param.put("limit",Integer.parseInt(limit));


            // 查询所有的商品信息
            // 返回数据就应该包含分页商品数据，分页的页数，分页的总条数
            ResultVO pageData = new PostServiceImpl().findPage(param);
            // 将返回值对象转换为JSON字符串，响应给浏览器
            // 使用三方的json工具类，直接别人写好的json转换方法来实现
            String jsonString = JSON.toJSONString(pageData);
            System.out.println("jsonString = " + jsonString);
            // 通过响应对象把数据返回给浏览器
//            response.getWriter().write(jsonString);
            response.getWriter().write(jsonString);
    }
    @RequestMapping("/post/findname")
    public void getUserName(HttpServletRequest request,HttpServletResponse response)throws IOException,ServerException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        // 通过json功能类来获取json数据
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);
        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.findUser((Integer) res.get("user_id"));
        resultVO.setMessage(user.getUsername());
        resultVO.setType(0);
        response.getWriter().write( JSON.toJSONString(resultVO));
    }
    @RequestMapping("/post/addp")
    public void addPrise(HttpServletRequest request,HttpServletResponse response)throws IOException,ServerException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        // 通过json功能类来获取json数据
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);
        Integer postid = new PostServiceImpl().addPraise((Integer) res.get("postid"));
        if (postid>0){
            resultVO.setType(0);
            response.getWriter().write( JSON.toJSONString(resultVO));
        }


    }
    @RequestMapping("/post/delp")
    public void delPrise(HttpServletRequest request,HttpServletResponse response)throws IOException,ServerException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        // 通过json功能类来获取json数据
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);
        Integer postid = new PostServiceImpl().delPraise((Integer) res.get("postid"));
        if (postid>0){
            resultVO.setType(0);
            response.getWriter().write( JSON.toJSONString(resultVO));
        }
    }
    @RequestMapping("/post/cha")
    public void cha(HttpServletRequest request,HttpServletResponse response)throws IOException,ServerException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        // 通过json功能类来获取json数据
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);
        List postList = new PostServiceImpl().finTabel((String) res.get("seekp"));

            resultVO.setType(0);
            resultVO.setData(postList);
            response.getWriter().write( JSON.toJSONString(resultVO));

    }
}
